//jqGrid的配置信息
$.jgrid.defaults.width = 1000;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';
$(function () {
    $("#jqGrid").jqGrid({
        url: '../generator/list',
        datatype: "json",
        colModel: [			
			{ label: '表名', name: 'tableName', width: 100, key: true },
			{ label: 'Engine', name: 'engine', width: 70},
			{ label: '表备注', name: 'tableComment', width: 100 },
			{ label: '创建时间', name: 'createTime', width: 100 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50,100,200],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "data.list",
            page: "data.currPage",
            total: "data.totalPage",
            records: "data.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

layui.use(['layer'], function(){
	var layer = layui.layer;
	$("#query").on("click", function(){
		$("#jqGrid").jqGrid('setGridParam',{ 
	        postData:{'tableName': $("#tableName").val()},
	        page:1 
	    }).trigger("reloadGrid");
	});
	$("#tableName").on("keyup", function(){
		$("#jqGrid").jqGrid('setGridParam',{ 
			postData:{'tableName': $("#tableName").val()},
			page:1 
		}).trigger("reloadGrid");
	});
	$("#generator").on("click", function(){
		var tableNames = $("#jqGrid").jqGrid('getGridParam', 'selarrrow');
		if(tableNames == null){
			layer.msg("请选择一条记录", {shade: 0.3});
			return ;
		}
		location.href = "../generator/code?tables=" + tableNames;
	});
	
})

//选择一条记录
function getSelectedRow() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	layer.msg("请选择一条记录", {shade: 0.3});
    	return ;
    }
    
    var selectedIDs = grid.getGridParam("selarrrow");
    if(selectedIDs.length > 1){
    	alert("只能选择一条记录");
    	return ;
    }
    
    return selectedIDs[0];
}

//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	layer.msg("请选择一条记录", {shade: 0.3});
    	return ;
    }
    return grid.jqGrid('getGridParam', 'selarrrow');
}